package com.doge.service.wx.collect;

import com.doge.bean.pojo.collect.Collect;
import com.doge.bean.pojo.collect.CollectExample;
import com.doge.bean.pojo.goods.Goods;
import com.doge.bean.vo.collect.CollectListForListVo;
import com.doge.bean.vo.collect.CollectListVo;
import com.doge.mapper.CollectMapper;
import com.doge.mapper.GoodsMapper;
import com.doge.util.auth.GetSubject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: doge
 * @description: 收藏服务
 * @author: Mr.Jiang
 * @create: 2021-12-02 20:33
 **/
@Service
public class WxCollectServiceImpl implements WxCollectService{
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public void addordelete(Integer valueId,Integer type) {
        CollectExample collectExample = new CollectExample();
        collectExample.createCriteria().andUserIdEqualTo(GetSubject.getUserId()).andValueIdEqualTo(valueId);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        if(collects.size()==0){
            Collect collect = new Collect();
            collect.setAddTime(new Date());
            collect.setDeleted(false);
            collect.setType(type.byteValue());
            collect.setUserId(GetSubject.getUserId());
            collect.setUpdateTime(new Date());
            collect.setValueId(valueId);
            collectMapper.insertSelective(collect);
        }else {
            Collect collect = new Collect();
            collect.setUpdateTime(new Date());
            collect.setDeleted(!collects.get(0).getDeleted());
            collectMapper.updateByExampleSelective(collect,collectExample);
        }
    }

    @Override
    public CollectListVo list(Integer type, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        CollectExample collectExample = new CollectExample();
        collectExample.createCriteria().andUserIdEqualTo(GetSubject.getUserId()).andDeletedEqualTo(false);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        ArrayList<CollectListForListVo> list = new ArrayList<CollectListForListVo>();
        for (Collect collect : collects) {
            Goods goods1 = goodsMapper.selectByPrimaryKey(collect.getValueId());
            CollectListForListVo collectListForListVo = new CollectListForListVo();
            collectListForListVo.setBrief(goods1.getBrief());
            collectListForListVo.setId(collect.getId());
            collectListForListVo.setName(goods1.getName());
            collectListForListVo.setPicUrl(goods1.getPicUrl());
            collectListForListVo.setRetailPrice(goods1.getRetailPrice());
            collectListForListVo.setType(Integer.valueOf(collect.getType()));
            collectListForListVo.setValueId(goods1.getId());
            list.add(collectListForListVo);
        }
        PageInfo pageInfo = new PageInfo(collects);
        CollectListVo collectListVo = new CollectListVo();
        collectListVo.setLimit(limit);
        collectListVo.setPage(page);
        collectListVo.setPages(pageInfo.getPages());
        collectListVo.setTotal((int) pageInfo.getTotal());
        collectListVo.setList(list);
        return collectListVo;
    }
}
